/*
Copyright (C) 2009-2010 Xanadu Development Team

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; version 2
of the License.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/

#include "../PacketCreator.h"
#include "../../server/Player.h"
#include "../../server/Quest.h"

PacketWriter* PacketCreator::startQuest(Quest* quest) {
	pw.writeShort(SHOW_STATUS_INFO);
	pw.write(1);
	pw.writeShort(quest->getID());
	pw.write(1);
	pw.writeInt(0);
	pw.writeInt(0);
	pw.writeShort(0);
	return &pw;
}

PacketWriter* PacketCreator::forfeitQuest(short questid) {
	pw.writeShort(SHOW_STATUS_INFO);
	pw.write(1);
	pw.writeShort(questid);
	pw.write(0);	
	return &pw;
}

PacketWriter* PacketCreator::completeQuest(Quest* quest) {
	pw.writeShort(SHOW_QUEST_COMPLETION);
	pw.write(1);
	pw.writeShort(quest->getID());
	pw.write(2);
	pw.writeLong(Tools::getLongTime());
    return &pw;
}

PacketWriter* PacketCreator::updateQuest(Quest* quest, int npcid, int nextquest) {
	pw.writeShort(UPDATE_QUEST_INFO);
	pw.write(8);
	pw.writeShort(quest->getID());
	pw.writeInt(npcid);
	pw.writeInt(nextquest);
	return &pw;
}

PacketWriter* PacketCreator::updateQuestInfo(Quest* quest, bool forfeit) {
	pw.writeShort(SHOW_STATUS_INFO);
	pw.write(1);
	pw.writeShort(quest->getID());
	pw.write(forfeit ? 0 : (quest->isCompleted() ? 2 : 1));
	if(!forfeit && quest->isCompleted()) {
		pw.writeLong(0); // Complete time
	} else {
		if(!forfeit)
			pw.writeString(quest->getKilledMobs());
		else
			pw.writeString("");
	}
	return &pw;
}

PacketWriter* PacketCreator::itemGainChat(int itemid, int amount) {
	pw.writeShort(SHOW_ITEM_GAIN_INCHAT);
	pw.write(3);
	pw.write(1);
	pw.writeInt(itemid);
	pw.writeInt(amount);
	return &pw;
}

PacketWriter* PacketCreator::mesosGainChat(int amount) {
	pw.writeShort(SHOW_STATUS_INFO);
	pw.write(5);
	pw.writeInt(amount);
	return &pw;
}

PacketWriter* PacketCreator::fameGainChat(int amount) {
	pw.writeShort(SHOW_STATUS_INFO);
	pw.write(4);
	pw.writeInt(amount);
	return &pw;
}
